package ui 
{
	import com.greensock.motionPaths.RectanglePath2D;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import org.flixel.data.FlxAnim;
	import org.flixel.FlxG;
	/**
	 * ...
	 * @author Jason Won
	 */
	public class AnimatedBitmap extends Bitmap
	{
		/**
		 * The width of the actual graphic or image being displayed (not necessarily the game object/bounding box).
		 * NOTE: Edit at your own risk!!  This is intended to be read-only.
		 */
		public var frameWidth:uint;
		/**
		 * The height of the actual graphic or image being displayed (not necessarily the game object/bounding box).
		 * NOTE: Edit at your own risk!!  This is intended to be read-only.
		 */
		public var frameHeight:uint;
		
		private var _pixels:BitmapData;
		private var _point:Point;		
		
		//Animation helpers
		protected var _animations:Array;
		protected var _curAnim:FlxAnim;
		protected var _curFrame:uint;
		protected var _caf:uint;
		protected var _frameTimer:Number;
		
		//Rendering Helpers
		protected var _rect:Rectangle;
		
		
		/**
		 * Whether the current animation has finished its first (or only) loop.
		 */
		public var finished:Boolean;
		
		public function AnimatedBitmap(bitmap:BitmapData, w:uint, h:uint, transparent:Boolean = true, pixelSnapping:String = "auto", smoothing:Boolean = false) 
		{
			super(new BitmapData(w, h, transparent, 0x000000), pixelSnapping, smoothing);
			_pixels = bitmap;
			
			finished = false;
			frameWidth = w;
			frameHeight = h;
			
			_animations = new Array();			
			_curAnim = null;			
			_curFrame = 0;
			_caf = 0;
			_frameTimer = 0;
			
			_point = new Point(0, 0);
			_rect = new Rectangle(0, 0, w, h);
			bitmapData.copyPixels(_pixels, _rect, _point);
		}
		
		public function addAnimation(name:String, frames:Array, frameRate:Number = 0, looped:Boolean = true):void
		{
			_animations.push(new FlxAnim(name, frames, frameRate, looped));
		}
		
		public function play(animName:String, force:Boolean = false):void
		{
			if(!force && (_curAnim != null) && (animName == _curAnim.name)) return;
			_curFrame = 0;
			_caf = 0;
			_frameTimer = 0;
			var al:uint = _animations.length;
			for(var i:uint = 0; i < al; i++)
			{
				if(_animations[i].name == animName)
				{
					_curAnim = _animations[i];
					if(_curAnim.delay <= 0)
						finished = true;
					else
						finished = false;
					_caf = _curAnim.frames[_curFrame];
					calcFrame();
					return;
				}
			}
		}
		
		/**
		 * Internal function to update the current animation frame.
		 */
		protected function calcFrame():void
		{
			var rx:uint = _caf*frameWidth;
			var ry:uint = 0;

			//Handle sprite sheets
			//var w:uint = _flipped?_flipped:_pixels.width;
			// Handle sprite sheets
			var w:uint = _pixels.width;
			if(rx >= w)
			{
				ry = uint(rx/w)*frameHeight;
				rx %= w;
			}
			
			//handle reversed sprites
			//if(_flipped && (_facing == LEFT))
			//	rx = (_flipped<<1)-rx-frameWidth;
			
			//Update display bitmap
			/*
			_flashRect.x = rx;
			_flashRect.y = ry;
			_framePixels.copyPixels(_pixels,_flashRect,_flashPointZero);
			_flashRect.x = _flashRect.y = 0;
			if(_ct != null) _framePixels.colorTransform(_flashRect,_ct);
			if(FlxG.showBounds)
				drawBounds();
			if(_callback != null) _callback(_curAnim.name,_curFrame,_caf);
			*/
			
			// Update display rectangle
			_rect = new Rectangle(rx, ry, frameWidth, frameHeight);
		}

		public function render():void
		{
			//_rect = new Rectangle(_caf * width, 0, width, height);
			bitmapData.copyPixels(_pixels, _rect, _point);
		}
			
		/**
		 * Internal function for updating the sprite's animation.
		 * Useful for cases when you need to update this but are buried down in too many supers.
		 * This function is called automatically by <code>FlxSprite.update()</code>.
		 */
		protected function updateAnimation():void
		{
			if((_curAnim != null) && (_curAnim.delay > 0) && (_curAnim.looped || !finished))
			{
				_frameTimer += FlxG.elapsed;
				if(_frameTimer > _curAnim.delay)
				{
					_frameTimer -= _curAnim.delay;
					if(_curFrame == _curAnim.frames.length-1)
					{
						if(_curAnim.looped) _curFrame = 0;
						finished = true;
					}
					else
						_curFrame++;
					_caf = _curAnim.frames[_curFrame];
					calcFrame();
				}
			}
		}
		
		
		/**
		 * Updates the frames for the bitmap
		 */
		public function update():void
		{
			updateAnimation();
		}
		
		/**
		 * Scales the image according to the given scales for the x and the y direction
		 * @param	scaleX
		 * @param	scaleY
		 */
		public function scale(scaleX:Number, scaleY:Number):void
		{
			
		}
		
		
		
		
	}

}